package com.cn.hjsj.appInterface.appImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cn.hjsj.appInterface.appService.AppRepairsService;
import com.cn.hjsj.pojo.Repairs;
import com.cn.hjsj.pojo.Times;
import com.cn.hjsj.service.IRepairsService;
import com.cn.hjsj.service.ITimesService;
import com.cn.hjsj.util.log.LogUtil;
import com.cn.hjsj.util.sequence.SeqFactory;
import com.cn.hjsj.util.time.TimeUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Component("appRepairsImpl")
public class AppRepairsImpl implements AppRepairsService{
	@Resource(name="repairsService")
	private IRepairsService repairsService;
	
	@Resource(name="timesService")
	private ITimesService timesService;
	@Override
	public Map<String, Object> subRepair(JSONObject object) {
		// TODO Auto-generated method stub
		Map<String, Object>  map=new HashMap<String,Object>(),
				mapTemp =new HashMap<String,Object>(),
				maps=new HashMap<String,Object>();
				
		try{

			//获取用户信息--调用mock接口获取用户信息


			Integer userId =1;

			maps.put("userName", "123");
			maps.put("tel", "123");
			maps.put("userId", userId);
			String orderNo = SeqFactory.GetCzSeq("Wx");
			//生成唯一订单号
			maps.put("orderNo", orderNo);
			//服务是上门取件时根据userId判断是否免费
			if(object.getInt("pickupCode") == 1 ){
				Times times = timesService.getTimesByUserId(userId);
				if(times.getServerTimes()<=0){
					mapTemp.put("orderNo", orderNo);
					mapTemp.put("prePay_id", "");//单号（微信必输）
					mapTemp.put("biz_content", "");//业务请求参数（支付宝必输）
					map.put("data", mapTemp);
				}
			}
						
			//选择服务类型--在提交订单是选择
			maps.put("serviceType", object.get("serviceType"));
		

//=======
//			maps.put("userName", "123");
//			maps.put("tel", "123");
//			maps.put("userId", "123");
//			
//>>>>>>> refs/remotes/origin/master
//			//生成唯一订单号
//			maps.put("orderNo", SeqFactory.GetCzSeq("Wx"));
//<<<<<<< HEAD

			//状态
			maps.put("parentStateCode", "PSC0002");
			maps.put("parentStateName", "审核");
			maps.put("statusCode", "CSC0002");
			maps.put("statusName", "待审核");

		
//=======
//>>>>>>> refs/remotes/origin/master
//			
//<<<<<<< HEAD
//			if(TimeUtil.daysBetween(object.getString("newTime"), TimeUtil.getNowStr())>365){
//				LogUtil.error("/AppRepairsImpl/subRepair:已过保，参数插入失败！");
//            	map.put("code", "40001");
//                map.put("mes", "已过保");
//                return map;
//			}

//=======
			//选择服务类型--在提交订单是选择
			maps.put("serviceType", object.get("serviceType"));
		
			
//			if(TimeUtil.daysBetween(object.getString("newTime"), TimeUtil.getNowStr())>365){
//				LogUtil.error("/AppRepairsImpl/subRepair:已过保，参数插入失败！");
//            	map.put("code", "40001");
//                map.put("mes", "已过保");
//                return map;
//			}
			
//			maps.put("parentStateCode", "123");
//			maps.put("parentStateName", "123");
//			maps.put("statusCode", "123");
//			maps.put("statusName", "123");
//>>>>>>> refs/remotes/origin/master
//			
			maps.put("productName", object.get("productName"));
			maps.put("newTime", object.get("newTime"));
			maps.put("subNumber", object.get("subNumber"));
			maps.put("description", object.get("description"));
			maps.put("photoName1", object.get("photoName1"));
			maps.put("voucher", object.get("voucher"));
			maps.put("pickupCode", object.get("pickupCode"));
			maps.put("pickupName", object.get("pickupName"));
			maps.put("pickupAddress", object.get("pickupAddress"));
			maps.put("deliveryAddress", object.get("deliveryAddress"));
			maps.put("siteCode", object.get("siteCode"));
			maps.put("siteTel", object.get("siteTel"));
			maps.put("createTime", TimeUtil.getNowStr());
			maps.put("updateTime", TimeUtil.getNowStr());
			if(object.containsKey("photoName2")&&!"".equals(object.get("photoName2"))){
				maps.put("photoName2", object.get("photoName2") );
			}
			
			if(object.containsKey("photoName3")&&!"".equals(object.get("photoName3"))){
				maps.put("photoName3", object.get("photoName3") );
			}
			
			if(object.containsKey("photoName4")&&!"".equals(object.get("photoName4"))){
				maps.put("photoName4",object.get("photoName4")  );
			}
			
			if(object.containsKey("photoName5")&&!"".equals(object.get("photoName5"))){
				maps.put("photoName5",object.get("photoName5")  );
			}
			
			if(object.containsKey("photoName6")&&!"".equals(object.get("photoName6"))){
				maps.put("photoName6",object.get("photoName6") );
			}
			
			int flag = repairsService.insertSelective(maps);
			if(flag!=1){
				LogUtil.error("/AppRepairsImpl/subRepair:参数插入失败！");
            	map.put("code", "40001");
                map.put("mes", "将数据绑定到数据库中失败");
                return map;
			}else{
				LogUtil.info("/AppRepairsImpl/subRepair:调用成功");
				map.put("code", "10000");
				map.put("mes", "调用成功,该服务单提交成功!");
				return map;
			}
		}catch(Exception e){
			LogUtil.error("/AppRepairsImpl/subRepair:解析参数异常！"+e.getMessage());
			map.put("code", "30000");
			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
			return map;
		}
	}
	@Override
	public Map<String,Object> getRepairsList(JSONObject object) {
		// TODO Auto-generated method stub
		Map<String, Object>  map=new HashMap<String,Object>(),
				mapOne=new HashMap<String,Object>(),
				maps=new HashMap<String,Object>(),
				mapTwo=new HashMap<String,Object>();
		JSONArray repairs = new JSONArray();
		try{
			//获取 ServiceType 列表
			Integer page = object.getInt("page");					
            Integer pageNo = object.getInt("linage");             		
            page = (page - 1) * pageNo;
            if(page<0){
                map.put("code", "30005");
                map.put("mes", "page有误");
                return map;
            }
            maps.put("startRow", page);
            maps.put("pageSize", pageNo);
            
			List<Repairs> repairsList = repairsService.getList(maps);
			int count = repairsService.getCount(maps);
			if(repairsList!=null){
				for (int i=0;i<repairsList.size();i++){
					mapOne.put("orderNo", repairsList.get(i).getOrderno());
					mapOne.put("productName", repairsList.get(i).getProductname());	
					mapOne.put("createTime", repairsList.get(i).getCreateTime());	
					mapOne.put("statusCode", repairsList.get(i).getStatuscode());	
					repairs.add(mapOne);
				}
				
				LogUtil.info("/AppServiceTypeImpl/getServiceType:调用成功");
				mapTwo.put("serviceType", repairs);
				mapTwo.put("count", count);
				map.put("code", "10000");
				map.put("data", mapTwo);
				map.put("mes", "调用成功");						
			}else{
				LogUtil.info("/AppServiceTypeImpl/getServiceType:调用成功");
				mapTwo.put("serviceType", repairs);
				mapTwo.put("count", count);
				map.put("code", "10000");
				map.put("data", mapTwo);
				map.put("mes", "调用成功,数据库中无数据");
			}
			return map;
		}catch(Exception e){
			LogUtil.error("/AppServiceTypeImpl/getServiceType:解析参数异常！"+e.getMessage());
			map.put("code", "30000");
			map.put("mes", "参数列表初始化错误，可能不是合法的JSON数据格式！");
		
			return map;
		}
	}

}
